/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

import { pickedLineData } from '@amcax/base';
import {
  PickInputRJSFFormat,
  NumberInputFormat,
  BasePickInputRJSFFormat,
} from '@app-cad/common/components/command-form/constants';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { RJSFSchema } from '@rjsf/utils';
import i18n from 'i18next';
import { useEffect, useState } from 'react';

type FormData = {
  curve: pickedLineData;
  density: number;
  scalar: number;
};

// 梳状图， 即 箭状曲率分析~
function CommandCurvatureComb({ title, onClose }: CurvedCmdDialogParams) {
  const initData = {
    curve: null,
    density: 20,
    scalar: 20,
  };
  const [formData, setFormData] = useState<FormData>(initData);

  useEffect(() => {
    preview();
  }, [formData]);

  const isFormDataValid = () => {
    const { curve, density, scalar } = formData;

    // 需要选择曲线
    const type = curve?.assisObj?.getType();
    if (type !== BasePickInputRJSFFormat.PickCurve) {
      // TODO: 做个提示？
      return false;
    }

    return curve != null && density > 0 && scalar != 0;
  };

  const { clear, confirmCommand } = useCommand('cmd-curve-join', {
    commandName: 'GetCurveComb',
    onClose,
  });

  const execute = async (preview = false) => {
    const { curve, density, scalar } = formData;
    return confirmCommand(
      {
        a: {
          curve: curve.edgeId,
          density: density,
          scalar: scalar,
        },
      },
      preview,
    );
  };

  const preview = async () => {
    clear();
    if (!isFormDataValid()) {
      return;
    }
    await execute(true);
  };

  const schema: RJSFSchema = {
    type: 'object',
    properties: {
      curve: {
        type: 'string',
        format: PickInputRJSFFormat.PickCurve,
      },
      density: {
        type: 'number',
        title: '密度',
        default: initData.density,
        format: NumberInputFormat.Integer,
      },
      scalar: {
        type: 'number',
        title: '缩放',
        default: initData.scalar,
        format: NumberInputFormat.Integer,
      },
    },
  };

  return (
    <PickableCommandForm
      onClose={onClose}
      title={title}
      schema={schema}
      formData={formData}
      onChange={(v) => {
        setFormData(v);
      }}
      extraUiSchema={{
        curve: {
          'ui:placeholder': '请选择曲线',
          'ui:autofocus': true,
        },
        density: {
          'ui:options': {
            disableParameter: false,
          },
        },
        scalar: {
          'ui:options': {
            disableParameter: false,
          },
        },
      }}
      submitText='完成'
      showSubmit={false}
      noValidate
      isFormDataValid={isFormDataValid()}
    />
  );
}

export const cmdCurvatureComb: CurvedBaseCommand = {
  id: 'CommandCurvatureComb',
  label: 'command.curve.curvature-comb',
  icon: 'commands/curved/curvature-comb',
  create: (label, onClose, props) => (
    <CommandCurvatureComb
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
